package xcmg.device.service.audit;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.DataDictionaryDO;
import xcmg.device.dao.entity.delivery.DeliveryReceiptDO;
import xcmg.device.dao.mapper.DataDictionaryMapper;
import xcmg.device.dao.mapper.delivery.DeliveryReceiptMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.DeliveryReceiptAuditStatusEnum;
import xcmg.device.infra.enums.DeliveryReceiptEnum;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.vo.delivery.BusinessOrderAuditParamVO;
import yb.ecp.fast.infra.infra.ActionResult;
import java.util.List;

@Service
public class DeliveryReceiptAuditService extends AbstractBusinessOrderAuditService{

    @Autowired
    private DeliveryReceiptMapper deliveryReceiptMapper;
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public ActionResult paramBusinessCheck(BusinessOrderAuditParamVO auditParam) {
        //查询审核的数据中的状态与当前操作做对比，判断是否可以进行当前操作
        List<String> ids = auditParam.getIds();
        List<DeliveryReceiptDO> deliveryReceipts = deliveryReceiptMapper.itemByIds(ids);
        if(CollectionUtils.isEmpty(deliveryReceipts)){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"未查询到记录");
        }
        //当勾选的数据包含审核通过、审核拒绝的数据，弹窗提示：发运交接单号：XXXXXX，处于审核通过状态，请重新选择！
        for(DeliveryReceiptDO receipt : deliveryReceipts){
            String auditStatus = receipt.getAuditStatus();
            if(StringUtils.isNotEmpty(auditStatus) && auditStatus.equals(DeliveryReceiptAuditStatusEnum.AUDIT_PASS.getAuditStatus().toString())){
                return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"发运交接单号："+receipt.getBillNo()+"，处于审核通过状态，请重新选择！");
            }
            if(StringUtils.isNotEmpty(auditStatus) && auditStatus.equals(DeliveryReceiptAuditStatusEnum.AUDIT_REJECT.getAuditStatus().toString())){
                return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"发运交接单号："+receipt.getBillNo()+"，处于审核驳回状态，请重新选择！");
            }
        }
        return ActionResultUtil.successActionResult();
    }

    @Override
    public void updateAuditStatus(BusinessOrderAuditParamVO auditParam, String userId, String userName,boolean pass) {
        //更新单据的审核状态
        List<String> ids = auditParam.getIds();
        if(pass){//审核通过
            DataDictionaryDO auditDataDictionaryDO = dataDictionaryMapper.itemByCode(DeliveryReceiptEnum.Status.Name.toString(),DeliveryReceiptAuditStatusEnum.AUDIT_PASS.getAuditStatus());
            String auditDesc = auditDataDictionaryDO == null ? "" : auditDataDictionaryDO.getName();
            String auditRemark = auditParam.getAuditRemark();
            deliveryReceiptMapper.updateAuditInfo(ids,DeliveryReceiptAuditStatusEnum.AUDIT_PASS.getAuditStatus(),userId,userName,auditDesc, auditRemark);
        }else{//审核拒绝
            DataDictionaryDO auditDataDictionaryDO = dataDictionaryMapper.itemByCode(DeliveryReceiptEnum.Status.Name.toString(), DeliveryReceiptAuditStatusEnum.AUDIT_REJECT.getAuditStatus());
            String auditDesc = auditDataDictionaryDO == null ? "" : auditDataDictionaryDO.getName();
            String auditRemark = auditParam.getAuditRemark();
            deliveryReceiptMapper.updateAuditInfo(ids,DeliveryReceiptAuditStatusEnum.AUDIT_REJECT.getAuditStatus(),userId,userName,auditDesc, auditRemark);
        }
        return ;
    }
}
